home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- ============================================================
- Example for sorting dates in the format d Month yyyy
- Use with: Dates3.xml
- Notes:-
- 1) The day portion of the date can be one or two digits
- therefore it is necessary to pad this with zeroes so that
- final sort select is a constant length numeric.
- 2) The month portion (specified as a month name) needs to
- be converted to a numeric value.
- ================================================================= -->
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="html" encoding="ISO-8859-1" indent="yes"/>
- <!-- define variable that is used to convert month names to numeric value -->
- <xsl:variable name="month-names" select="'|January|February|March|April|May|June|July|August|September|October|November|December'"/>
- <xsl:variable name="month-values" select="'|01-----|02------|03---|04---|05-|06--|07--|08----|09-------|10-----|11------|12------'"/>
- <xsl:template match="/">
- <html><body>
- <table border="1">
- <tr>
- <th>Sorted Position</th>
- <th>Sort value</th>
- <th>Original date value</th>
- <th>Original position</th>
- </tr>
- <xsl:for-each select="/root/item">
- <!-- sort algorithm -->
- <xsl:sort select="concat(
- substring-after(substring-after(@date,' '),' '),
- substring($month-values,string-length(substring-before($month-names,concat('|',substring-before(substring-after(@date,' '),' '))))+2,2),
- substring('00',1,2-string-length(substring-before(@date,' '))),substring-before(@date,' ')
- )" data-type="number"/>
- <tr>
- <td>
- <xsl:value-of select="position()"/>
- </td>
- <td>
- <!-- the year portion -->
- <xsl:value-of select="substring-after(substring-after(@date,' '),' ')"/>
- <!-- the month portion -->
- <xsl:value-of select="substring($month-values,string-length(substring-before($month-names,concat('|',substring-before(substring-after(@date,' '),' '))))+2,2)"/>
- <!-- the day portion -->
- <xsl:value-of select="concat(substring('00',1,2-string-length(substring-before(@date,' '))),substring-before(@date,' '))"/>
- </td>
- <td>
- <xsl:value-of select="@date"/>
- </td>
- <td>
- <!-- show the order of the element in the original xml -->
- <xsl:value-of select="count(preceding-sibling::item)+1"/>
- </td>
- </tr>
- </xsl:for-each>
- </table>
- </body></html>
- </xsl:template>
- </xsl:stylesheet>